package com.datn.activity;

import java.util.ArrayList;

import com.datn.DataAccess.FavouriteDatabaseAdapter;
import com.datn.DataAccess.ListPlaceAdapter;
import com.datn.DataAccess.ListRecentAdapter;
import com.datn.beans.FavouriteBean;
import com.datn.beans.UserBean;
import com.datn.utils.DialogUtils;
import com.datn.utils.PreferenceUtils;
import com.datn.utils.places.Place;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class RecentPlaceActivity extends Activity implements OnItemClickListener{

	public static final String DELETE_CONTEXT_MENU = "Delete";
	
	ArrayList<FavouriteBean> allRecentPlace;
	ListRecentAdapter adapterList;
	ListView lvRecent;
	Context mContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recent_place);
		PreferenceUtils.getInstance(this);
		mContext = this;
		lvRecent=(ListView) findViewById(R.id.lvRecentPlace);
		updateListRecentPlace();
		lvRecent.setOnItemClickListener(this);
		lvRecent.setOnCreateContextMenuListener(this);
	}
	
	public void updateListRecentPlace(){
		if (UserBean.isLogin){
			FavouriteDatabaseAdapter adapterDatabase = new FavouriteDatabaseAdapter(this);
			adapterDatabase.open();
			allRecentPlace = adapterDatabase.getRecentPlaceWithUser(UserBean.username); 
			adapterList=new ListRecentAdapter(this, R.layout.item_recent_place, allRecentPlace); //ListPlaceAdapter(mContext, R.layout.item_place, array);			
			lvRecent=(ListView) findViewById(R.id.lvRecentPlace);
			lvRecent.setAdapter(adapterList);
			adapterDatabase.close();
		}
		else{
			Toast.makeText(this, "You need login before using this function", Toast.LENGTH_LONG).show();
		}
	}
	
	@Override
    public void onCreateContextMenu(ContextMenu contextMenu,
                                    View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
		if (v.getId()==R.id.lvRecentPlace) {
		    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		    contextMenu.setHeaderTitle("Quick Menu");
		    contextMenu.add(Menu.NONE,0,0,DELETE_CONTEXT_MENU);
		  }
    }
	
	@Override
    public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		FavouriteDatabaseAdapter adapter = new FavouriteDatabaseAdapter(this);
		try{
		
		adapter.open();
			int result = adapter.deleteRecentPlace(allRecentPlace.get(info.position));
			if (result<=0) {
				Toast.makeText(this, "Delete failed", Toast.LENGTH_LONG).show();
			}
			else{
				Toast.makeText(this, "Delete success", Toast.LENGTH_LONG).show();
				updateListRecentPlace();
			}
			
		adapter.close();
		}
		catch(Exception ex){
			
		} finally {
			try {
				adapter.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
        return false;
    }

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if (keyCode==KeyEvent.KEYCODE_BACK){
			Intent intent=new Intent(this, NearLocationActivity.class);
			startActivity(intent);
			finish();
		}
		
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		FavouriteBean bean = allRecentPlace.get(position);
		Intent intent = new Intent(this, DetailsPlaceActivity.class);
		PreferenceUtils.setModeOnlinẹ̣(false);
		Place place = new Place();
			place.setName(bean.getNameplace());
			place.setLatitude(bean.getLatitude());
			place.setLongtitude(bean.getLongitude());
			place.setAddressLine(bean.getAddress());
			place.setActualurl(bean.getUrl());
			place.setPhonenumber(bean.getPhonenumber());
		PreferenceUtils.setPlaceDetails(place);
		startActivity(intent);
		finish();
	}
	
	
	 @Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// TODO Auto-generated method stub
	    	MenuInflater inflater = getMenuInflater();
	    	inflater.inflate(R.menu.recent_menu, menu);
			return true;
		}


		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			// TODO Auto-generated method stub
			switch (item.getItemId()) {
			case R.id.itemHelpRecentMenu:
				processClickHelpMenu();			
				break;
			default:
				break;
			}
			return super.onOptionsItemSelected(item);
		}

		private void processClickHelpMenu() {
			// TODO Auto-generated method stub
			DialogUtils.getInstance(this);
			DialogUtils.showCustomHelpDialog(R.layout.help_recent_menu);
		}

}
